package com.bjpowernode.boss.mvc.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjpowernode.boss.PowerBossApplication;
import com.bjpowernode.boss.domain.PayWay;
import com.bjpowernode.boss.enums.ajax.Code;
import com.bjpowernode.boss.enums.buz.PublicStatus;
import com.bjpowernode.boss.exception.BossException;
import com.bjpowernode.boss.mapper.PayWayMapper;
import com.bjpowernode.boss.mvc.model.query.PayWayQuery;
import com.bjpowernode.boss.mvc.model.vo.DitOptionVo;
import com.bjpowernode.boss.mvc.service.PayWayService;
import com.bjpowernode.boss.utils.NoUtil;
import jakarta.annotation.Resource;
import org.springframework.context.PayloadApplicationEvent;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 支付方式 业务实现类
 */
@Service
public class PayWayServiceImpl extends ServiceImpl<PayWayMapper, PayWay> implements PayWayService {

    @Resource
    private PayWayMapper payWayMapper;

    @Override
    public List<DitOptionVo> loadPayWayOption() {
        return PowerBossApplication.DIC_PAY_WAY_LIST;
    }

    @Override
    public List<DitOptionVo> loadPayTypeOption(String wayCode) {
        Map<String, List<DitOptionVo>> dicPayTypeMap = PowerBossApplication.DIC_PAY_TYPE_MAP;
        return dicPayTypeMap.get(wayCode);
    }

    @Override
    public void addPayWayConfig(PayWayQuery payWayQuery) {
        // 判断 数据库中 是否存在
        PayWay payWay = payWayMapper.selectOne(
                new LambdaQueryWrapper<PayWay>()
                        .eq(PayWay::getPayProductCode, payWayQuery.getProductCode())
                        .eq(PayWay::getWayCode, payWayQuery.getWayCode())
                        .eq(PayWay::getTypeCode, payWayQuery.getTypeCode())
        );
        if (payWay != null) {
            throw new BossException(Code.PAY_WAY_CONFIG_EXIST);
        }

        // 不存在 , 进行存值
        payWay = new PayWay();
        payWay.setId(NoUtil.uuid());
        payWay.setWayCode(payWayQuery.getWayCode());
        payWay.setWayName(PowerBossApplication.DIC_PAY_WAY_NAME_MAP.get(payWayQuery.getWayCode()));
        payWay.setTypeCode(payWayQuery.getTypeCode());
        payWay.setTypeName(PowerBossApplication.DIC_PAY_TYPE_NAME_MAP.get(payWayQuery.getWayCode() + ":" + payWayQuery.getTypeCode()));
        payWay.setPayProductCode(payWayQuery.getProductCode());
        payWay.setPayRate(new BigDecimal(payWayQuery.getPayRate()));
        payWay.setSorts(Integer.valueOf(payWayQuery.getSorts()));
        payWay.setStatus(PublicStatus.ENABLE.name());
        payWay.setEditTime(new Date());
        payWay.setRemark("创建支付方式");
        payWayMapper.insert(payWay);
    }

    @Override
    public IPage<PayWay> loadWayTablePageData(Integer pageNo, Integer pageSize, String productCode) {
        LambdaQueryWrapper<PayWay> queryWrapper = new LambdaQueryWrapper<PayWay>()
                .orderByAsc(PayWay::getPayProductCode, PayWay::getSorts)
                .orderByDesc(PayWay::getEditTime);
        // 判断productCode 是否为空
        if (StrUtil.isNotBlank(productCode)) {
            queryWrapper.eq(PayWay::getPayProductCode, productCode);
        }
        return payWayMapper.selectPage(new Page<>(pageNo, pageSize), queryWrapper);
    }

    @Override
    public void removePayWayConfig(String productCode, String wayCode, String typeCode) {
        // 删除
        int delete = payWayMapper.delete(new LambdaQueryWrapper<PayWay>()
                .eq(PayWay::getPayProductCode, productCode)
                .eq(PayWay::getWayCode, wayCode)
                .eq(PayWay::getTypeCode, typeCode));
        if (delete == 0) {
            throw new BossException(Code.PAY_WAY_NOT_FOUNT);
        }
    }
}
